package com.cencat.driver.entity;

import com.baomidou.mybatisplus.annotation.*;
import com.cencat.common.entity.BaseEntity;
import jakarta.validation.constraints.*;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.time.LocalDateTime;

/**
 * 司机消息实体类
 * 
 * @author cencat
 * @since 2024-01-01
 */
@Data
@EqualsAndHashCode(callSuper = true)
@TableName("driver_message")
public class DriverMessage extends BaseEntity {

    /**
     * 消息ID
     */
    @TableId(value = "id", type = IdType.AUTO)
    private Long id;

    /**
     * 消息类型：1-系统通知，2-任务通知，3-订单通知，4-紧急通知
     */
    @NotNull(message = "消息类型不能为空")
    @Min(value = 1, message = "消息类型值不能小于1")
    @Max(value = 4, message = "消息类型值不能大于4")
    @TableField("message_type")
    private Integer messageType;

    /**
     * 消息标题
     */
    @NotBlank(message = "消息标题不能为空")
    @Size(max = 100, message = "消息标题长度不能超过100个字符")
    @TableField("title")
    private String title;

    /**
     * 消息内容
     */
    @NotBlank(message = "消息内容不能为空")
    @TableField("content")
    private String content;

    /**
     * 司机ID
     */
    @NotNull(message = "司机ID不能为空")
    @TableField("driver_id")
    private Long driverId;

    /**
     * 关联ID（任务ID、订单ID等）
     */
    @TableField("related_id")
    private Long relatedId;

    /**
     * 是否已读：0-未读，1-已读
     */
    @Min(value = 0, message = "已读状态值不能小于0")
    @Max(value = 1, message = "已读状态值不能大于1")
    @TableField("is_read")
    private Integer isRead;

    /**
     * 阅读时间
     */
    @TableField("read_time")
    private LocalDateTime readTime;

    /**
     * 优先级：1-低，2-中，3-高，4-紧急
     */
    @NotNull(message = "优先级不能为空")
    @Min(value = 1, message = "优先级值不能小于1")
    @Max(value = 4, message = "优先级值不能大于4")
    @TableField("priority")
    private Integer priority;

    /**
     * 过期时间
     */
    @TableField("expire_time")
    private LocalDateTime expireTime;

    /**
     * 租户ID
     */
    @NotNull(message = "租户ID不能为空")
    @TableField("tenant_id")
    private Long tenantId;

    // 消息类型枚举常量
    public static final int TYPE_SYSTEM = 1;     // 系统通知
    public static final int TYPE_TASK = 2;       // 任务通知
    public static final int TYPE_ORDER = 3;      // 订单通知
    public static final int TYPE_EMERGENCY = 4;  // 紧急通知

    // 已读状态枚举常量
    public static final int READ_STATUS_UNREAD = 0;  // 未读
    public static final int READ_STATUS_READ = 1;    // 已读

    // 优先级枚举常量
    public static final int PRIORITY_LOW = 1;       // 低
    public static final int PRIORITY_MEDIUM = 2;    // 中
    public static final int PRIORITY_HIGH = 3;      // 高
    public static final int PRIORITY_URGENT = 4;    // 紧急

    /**
     * 获取消息类型描述
     */
    public String getMessageTypeDesc() {
        switch (this.messageType) {
            case TYPE_SYSTEM: return "系统通知";
            case TYPE_TASK: return "任务通知";
            case TYPE_ORDER: return "订单通知";
            case TYPE_EMERGENCY: return "紧急通知";
            default: return "未知";
        }
    }

    /**
     * 获取优先级描述
     */
    public String getPriorityDesc() {
        switch (this.priority) {
            case PRIORITY_LOW: return "低";
            case PRIORITY_MEDIUM: return "中";
            case PRIORITY_HIGH: return "高";
            case PRIORITY_URGENT: return "紧急";
            default: return "未知";
        }
    }

    /**
     * 判断是否已读
     */
    public boolean isRead() {
        return READ_STATUS_READ == this.isRead;
    }

    /**
     * 判断是否未读
     */
    public boolean isUnread() {
        return READ_STATUS_UNREAD == this.isRead;
    }

    /**
     * 判断是否已过期
     */
    public boolean isExpired() {
        if (expireTime == null) {
            return false;
        }
        return LocalDateTime.now().isAfter(expireTime);
    }

    /**
     * 判断是否为紧急消息
     */
    public boolean isUrgent() {
        return PRIORITY_URGENT == this.priority;
    }

    /**
     * 判断是否为高优先级消息
     */
    public boolean isHighPriority() {
        return PRIORITY_HIGH == this.priority || PRIORITY_URGENT == this.priority;
    }

    /**
     * 标记为已读
     */
    public void markAsRead() {
        this.isRead = READ_STATUS_READ;
        this.readTime = LocalDateTime.now();
    }

    /**
     * 标记为未读
     */
    public void markAsUnread() {
        this.isRead = READ_STATUS_UNREAD;
        this.readTime = null;
    }
}